
import java.util.Collections;
public class EdgesTable {

  ArrayList table;
  int minY = 1<<30;
  int maxY = 0;
  boolean sorted = false;


  public EdgesTable() {
    this.table = new ArrayList<Edge>();
  }

  public EdgesTable(ArrayList<PVector> vertex) {
    this();
    if (vertex.size()>1) {
      int x1, y1;
      int x0 = (int)vertex.get(0).x;
      int y0 = (int)vertex.get(0).y; 
      for (int i=1 ; i <= vertex.size(); i++) {
        x1 = (int)vertex.get(i%vertex.size()).x;
        y1 = (int)vertex.get(i%vertex.size()).y;
        minY = (y0 < minY)?y0:minY;
        maxY = (y0 > minY)?y0:maxY;
        if(y0!=y1)table.add(new Edge(x0, y0, x1, y1));
        x0 = x1;
        y0 = y1;
      }
    }
    sorted = false;
  }

  public ArrayList<Edge> getActiveEdgeTable(int y) {
    if (!sorted) { 
      Collections.sort(table); 
      sorted = true;
    }
    ArrayList<Edge> activeEdgesTable = new ArrayList<Edge>();
    for (int i=0; i < table.size(); i++) {
      if (((Edge)table.get(i)).minY == y) {
        activeEdgesTable.add((Edge)table.get(i));
      }
    }
    return activeEdgesTable; 
  }

  public int getMinY() {
    return minY;
  }

  public int getMaxY() {
    return maxY;
  }

  public int size() {
    return table.size();
  }
}

